package com.shuoyue.ycgk.ui.common;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.entity.DayTaget;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import io.reactivex.Observable;

public interface TagetContract {
    interface View extends BaseView {
        void setTodayTaget(DayTaget dayTaget);

        void userSetTodayTaget();
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();

        public void getTaget(int modelId, String day) {
            apply(model.getDayTaget(modelId, day))
                    .subscribe(new ApiSubscriber<Optional<DayTaget>>(mView, this) {
                        @Override
                        public void onNext(Optional<DayTaget> dayTagetOptional) {
                            super.onNext(dayTagetOptional);
                            mView.setTodayTaget(dayTagetOptional.getIncludeNull());
                        }
                    });
        }

        public void setDayTaget(int modelId, int setMinuteNum, int setQuestionNum, int setAccuracyNum) {
            apply(model.setDayTaget(modelId, setAccuracyNum, setMinuteNum, setQuestionNum))
                    .subscribe(new ApiSubscriber<Optional<String>>(mView, this) {
                        @Override
                        public void onNext(Optional<String> dayTagetOptional) {
                            super.onNext(dayTagetOptional);
                            mView.userSetTodayTaget();
                        }
                    });
        }

    }

    class Model extends BaseModel {
        Observable<BaseResult<DayTaget>> getDayTaget(int modelId, String targetDay) {
            return RetrofitClient.getInstance().getApi().tagetByDay(modelId, targetDay);
        }

        Observable<BaseResult<String>> setDayTaget(int modelId, int setAccuracyNum, int setMinuteNum, int setQuestionNum) {
            return RetrofitClient.getInstance().getApi().setDayTaget(modelId, setAccuracyNum, setMinuteNum, setQuestionNum);
        }


    }
}
